﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DLV.Data.Entities;
using DLV.FrameWork;
using DLV.Library.Utilities;

namespace DLV.BackEnd.Pages
{
    public partial class EditTourFull : AdminPageBase
    {
        private TourFullInfo _tourFullInfo = new TourFullInfo();
        private readonly TourFullController _tourFullController = new TourFullController();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Request.QueryString["TourFullId"]))
                _tourFullInfo = _tourFullController.GetTourFull(int.Parse(Request.QueryString["TourFullId"]));

            if (Page.IsPostBack) return;

            LoadStatus();
            LoadAdminUser();
            LoadTour();
            LoadTourPackage();
            LoadDateQuantity();

            if (_tourFullInfo.TourFullId != -1)
                RenderForm();
        }

        protected void lbtSave_Click(object sender, EventArgs e)
        {
            SaveData();
        }
        protected void lbtClose_Click(object sender, EventArgs e)
        {
            Response.Redirect("/Pages/ListTourFull.aspx?xml=TourFull");
        }

        private void SaveData()
        {
            _tourFullInfo.DateQuantity = ddlDateQuantity.SelectedValue;
            _tourFullInfo.TourId = int.Parse(ddlTour.SelectedValue);
            _tourFullInfo.TourPackageId = int.Parse(ddlTourPackage.SelectedValue);
            _tourFullInfo.Status = int.Parse(ddlStatus.SelectedValue);
            if (_tourFullInfo.TourFullId == -1)
            {
                _tourFullInfo.CreatedBy = ddlAdminUser.SelectedValue == "-1"
                    ? UserId
                    : int.Parse(ddlAdminUser.SelectedValue);
                _tourFullInfo.CreatedDate = DateTime.Now;
                _tourFullController.InsertTourFull(_tourFullInfo);
            }
            else
            {
                _tourFullInfo.UpdatedBy = UserId;
                _tourFullInfo.UpdatedDate = DateTime.Now;
                _tourFullController.UpdateTourFull(_tourFullInfo);
            }
            Response.Redirect("/Pages/ListTourFull.aspx?xml=TourFull");
        }

        private void RenderForm()
        {
            ddlDateQuantity.SelectedValue = _tourFullInfo.DateQuantity;
            ddlTour.SelectedValue = _tourFullInfo.TourId.ToString();
            ddlTourPackage.SelectedValue = _tourFullInfo.TourPackageId.ToString();
            ddlStatus.SelectedValue = _tourFullInfo.Status.ToString();
            ddlAdminUser.SelectedValue = _tourFullInfo.CreatedBy.ToString();
        }

        private void LoadDateQuantity()
        {
            for (var i = 1; i <= 30; i++)
            {
                var num = i < 10 ? "Ngày 0" + i : "Ngày " + i;
                ddlDateQuantity.Items.Add(new ListItem(num, num));
            }

            ddlDateQuantity.Items.Insert(0, new ListItem("--- Chọn ngày ---", "-1"));
        }

        private void LoadTour()
        {
            ddlTour.DataSource =
                new TourController().ListTour()
                    .Where(p => p.Status == (int) Globals.DocumentStatus.Use)
                    .OrderBy(p => p.TourName);
            ddlTour.DataTextField = "TourName";
            ddlTour.DataValueField = "TourId";
            ddlTour.DataBind();

            ddlTour.Items.Insert(0, new ListItem("--- Chọn tour ---", "-1"));
        }

        private void LoadTourPackage()
        {
            ddlTourPackage.DataSource =
                new TourPackageController().ListTourPackage()
                    .Where(p => p.Status == (int)Globals.DocumentStatus.Use)
                    .OrderBy(p => p.TourPackageName);
            ddlTourPackage.DataTextField = "TourPackageName";
            ddlTourPackage.DataValueField = "TourPackageId";
            ddlTourPackage.DataBind();

            ddlTourPackage.Items.Insert(0, new ListItem("--- Chọn gói tour ---", "-1"));
        }

        private void LoadAdminUser()
        {
            ddlAdminUser.DataSource = new AdminUserController().ListAdminUser();
            ddlAdminUser.DataTextField = "Username";
            ddlAdminUser.DataValueField = "UserID";
            ddlAdminUser.DataBind();

            ddlAdminUser.Items.Insert(0, new ListItem("--- Chọn người tạo ---", "-1"));
        }

        private void LoadStatus()
        {
            var ds = new DataSet();
            ds.ReadXml(MapPath("~/xml/Status.xml"));
            var dv = ds.Tables["Status"].DefaultView;
            ddlStatus.DataTextField = "Name";
            ddlStatus.DataValueField = "ID";
            ddlStatus.DataSource = dv;
            ddlStatus.DataBind();
        }
    }
}